--
-- @Author:      name
-- @DateTime:    2018-03-30 23:05:48
-- @Description: 消息的处理

local skynet = require "skynet"
local NodeMessage = require "NodeMessage"

local Command = class("Command")

---------------------------------------------------------
-- Private
---------------------------------------------------------
function Command:ctor(message)
	self.msg = message
	self.broadcastList = {} --广播列表
	self.nodeMessage = NodeMessage.new()
	self:register()

	local function loop()
		self:mainLoop()
	end
	skynet.fork(function ()
		while true do
			loop()
			skynet.sleep(400) 
		end
	end)

end

--注册本服务里的消息
function Command:register()

	self.msg:registerSelf('add_broadcast',handler(self,self.addBroadcast))
	self.msg:registerSelf('send_bulletin',handler(self,self.sendBulletin))	
end

function Command:mainLoop()
	-- print("______Command___mainLoop__________",self.broadcastList[1])
	if self.broadcastList[1] then	
		self.nodeMessage:broadcastAll(self.broadcastList[1])
		table.remove(self.broadcastList,1)
	end
end
---------------------------------------------------------
-- CMD
---------------------------------------------------------
function Command:addBroadcast( pack )

	table.insert(self.broadcastList,pack)
	-- print("______Command___addBroadcast__________",self.broadcastList[1])
	return true
end

function Command:send_bulletin( pack )
	self.nodeMessage:broadcastAll(pack)
	return true
end



return Command